package com.hkzhao.shixun.controller;


import com.hkzhao.shixun.common.api.CommonPage;
import com.hkzhao.shixun.common.api.CommonResult;
import com.hkzhao.shixun.dto.CoreLoginParam;
import com.hkzhao.shixun.dto.LotteryDetailDto;
import com.hkzhao.shixun.dto.XwoUserDto;
import com.hkzhao.shixun.dto.rtPrize;
import com.hkzhao.shixun.mbg.model.Activity;
import com.hkzhao.shixun.mbg.model.Lotteryrecord;
import com.hkzhao.shixun.mbg.model.Prize;
import com.hkzhao.shixun.service.AdminService;
import com.hkzhao.shixun.service.impl.StatServiceImpl;
import com.hkzhao.shixun.service.XwoUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <h3>j2ee</h3>
 * <p>xwo用户的api</p>
 *
 * @author : hkzhao
 * @date : 2020-06-05 16:44
 **/
@RestController
@Api(tags = "xwoUser",description = "xwoUser接口列表")
@RequestMapping("/xwoUser")
public class XwoUserController {
    @Autowired
    private XwoUserService xwoUserService;
    @Autowired
    private StatServiceImpl statService;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private AdminService adminService;
    @ApiOperation("xwo注册接口")
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public CommonResult regiter(@RequestBody XwoUserDto xwoUserDto){
       return xwoUserService.register(xwoUserDto.getUsername()+"_usr",xwoUserDto.getPassword(),xwoUserDto.getPhoneNumber());
    }

    @ApiOperation("xwo登录接口")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public CommonResult login(@RequestBody CoreLoginParam coreLoginParam, BindingResult bindingResult){
        String token = xwoUserService.login(coreLoginParam.getUsername()+"_usr",coreLoginParam.getPassword());
        if(token==null){
            return CommonResult.validateFailed("用户密码名错误");
        }
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        tokenMap.put("tokenHead",tokenHead);
        return CommonResult.success(tokenMap);
    }

    @ApiOperation("填写中奖具体信息")
    @RequestMapping(value = "/fillLotteryRecord",method = RequestMethod.POST)
    public CommonResult fillLotteryRecord(@RequestBody LotteryDetailDto lotteryDetailDto){
      return  xwoUserService.addLotteryRecord(lotteryDetailDto.getRecordId(),lotteryDetailDto.getLocation(),
                lotteryDetailDto.getActualName(),lotteryDetailDto.getPhoneNumber());
    }

    @ApiOperation("返回用户在某个活动中的中奖信息")
    @RequestMapping(value = "/getLotterRecord",method = RequestMethod.GET)
    public CommonResult<CommonPage<Lotteryrecord>> getLotterRecord(@RequestParam @ApiParam("活动的id")String activityId, HttpServletRequest httpServletRequest,
                                                                   @RequestParam @ApiParam("页码") int pageNum,
                                                                   @RequestParam @ApiParam("页面的大小") int pageSize){
        List<Lotteryrecord> lotteryrecordList = xwoUserService.findLotteryRecord(xwoUserService.getXwoUserNameByToken(httpServletRequest),activityId,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(lotteryrecordList));
    }

    @ApiOperation("返回用户的抽奖结果")
    @RequestMapping(value = "/draws",method = RequestMethod.GET)
    public CommonResult draws(@RequestParam @ApiParam("活动id") String activityId,HttpServletRequest httpServletRequest){
        statService.plus(activityId);
        return xwoUserService.DrawsLottery(httpServletRequest,activityId);
    }
    @ApiOperation("获取正在进行活动(user)")
    @RequestMapping(value="/getactivity",method=RequestMethod.GET)
    public CommonResult getactivity(@RequestParam(required=false) String activityid,@RequestParam(required=false) String tag,@RequestParam(required=false) String title,
                                    @RequestParam(required=false) String status,@RequestParam @ApiParam("页码") int pageNum, @RequestParam @ApiParam("页面的大小") int pageSize)
    {
        List<Activity> res=xwoUserService.getActivityby(activityid,tag,title,status,pageNum,pageSize);
        if (res.size()==0) return CommonResult.failed("没有记录");
        List<Map<String,Object>> vres=new ArrayList<>();
        for (Activity t:res)
        {
            Map<String,Object> mp=new HashMap<>();
            mp.put("activityid",t.getActivityid());
            mp.put("title",t.getTitle());
            mp.put("status",t.getStatus());
            vres.add(mp);
        }
        return CommonResult.success(CommonPage.restPage(vres));
    }
    @ApiOperation("获取奖品(根据活动id)")
    @RequestMapping(value="/getprize",method=RequestMethod.GET)
    public CommonResult getprize(String activityid)
    {
        List<Prize> res=adminService.getPrizeby(activityid);
        if (res.size()==0) return CommonResult.failed("没有记录");
        List<rtPrize> res2=new ArrayList<>();
        for (Prize x:res)
        {
            rtPrize tmp=new rtPrize();
            tmp.setLevel(x.getLevel());
            tmp.setPrizeid(x.getPrizeid());
            res2.add(tmp);
        }
        return CommonResult.success(res2);
    }
}

